<!DOCTYPE HTML>
<html>
<head>
<include file="Public:head" />
</head>
<body>
<!--main-->
<div class="midder">
<include file="top_menu" />
<include file="action_menu" />
<form id="export-form" method="post" action="{:U('startbackup')}">
<table  cellpadding="0" cellspacing="0">
<tr class="old">
    <td width="20"><input name="chkall" onclick="ToggleCheck(this)" type="checkbox" checked="chedked" ></td>
    <td>表名</td>
    <td>数据量</td>
    <td>数据大小</td>
    <td>创建时间</td>
    <td>备份状态</td>
    <td>操作</td>
</tr>

<volist name="list" id="table">
<tr class="odd">
    <td><input type="checkbox" name="tables[]" value="{$table.name}" checked="chedked" ></td>
    <td>{$table.name}</td>
    <td>{$table.rows}</td>
    <td>{$table.data_length|format_bytes}</td>
    <td>{$table.create_time}</td>
    <td class="info">未备份</td>
    <td class="action">
        <a class="ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
        <a class="ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}">修复表</a>
    </td>    
</tr>
</volist>

</table>
</form>
<include file="action_menu" />
<div class="pagebar">{$pageUrl}</div>
</div>
<script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables ,
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
                    alert(data.info);
                } else {
                    alert(data.info);
                }
                setTimeout(function(){
	                $('#top-alert').find('button').click();
	                $(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });
		
		//立即备份
        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("<span>正在发送备份请求...</span>");
            $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function(data){
                        if(data.status){
                            tables = data.tables;
                            $export.html(data.info + "开始备份，请不要关闭本页面！");
                            backup(data.tab); //开始
                            window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                        } else {
                            alert(data.info);
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");
                            setTimeout(function(){
            	                $('#top-alert').find('button').click();
            	                $(that).removeClass('disabled').prop('disabled',false);
            	            },1500);
                        }
                    },
                    "json"
            );           
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);
					//判断是否是纯对象
                  if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    alert(data.info);
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
    	                $('#top-alert').find('button').click();
    	                $(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
</script>
</body>
</html>